package org.black.phoneUtility.sms.schedule.app.activity;

import java.util.Calendar;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleAddEditFragment;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleAddEditMessageFragment;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleAddRecipientsFromUserInputDialogFragment;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleAddRecipientsFromUserInputDialogFragment.SmsScheduleAddRecipientsFromUserInputDialogInterface;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleChooseTagDialogFragment;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleDateTimeDatePickerDialogFragment.SmsScheduleDateTimeDatePickerDialogInterface;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleDateTimeTimePickerDialogFragment.SmsScheduleDateTimeTimePickerDialogFragmentInterface;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleRecipientsAddRecipientsDialogFragment.SmsScheduleRecipientsAddRecipientsDialogInterface;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleRecipientsListFragment;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleRepeatEndAfterDialogFragment.SmsScheduleRepeatEndAfterDialogInterface;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleRepeatEndOnSpecificDateDatePickerDialogFragment.SmsScheduleRepeatEndOnSpecificDateDatePickerDialogInterface;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleRepeatEveryDialogFragment.SmsScheduleRepeatEveryDialogInterface;
import org.black.phoneUtility.sms.schedule.app.fragment.SmsScheduleRepeatFragment;
import org.black.phoneUtility.sms.schedule.provider.SmsScheduleContract;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.view.MenuItem;

public class SmsScheduleAddEditFragmentActivity extends Activity
		implements
		SmsScheduleDateTimeDatePickerDialogInterface,
		SmsScheduleDateTimeTimePickerDialogFragmentInterface,
		SmsScheduleRecipientsAddRecipientsDialogInterface,
		SmsScheduleAddRecipientsFromUserInputDialogInterface,
		SmsScheduleRepeatEveryDialogInterface,
		SmsScheduleRepeatEndAfterDialogInterface,
		SmsScheduleRepeatEndOnSpecificDateDatePickerDialogInterface,
		SmsScheduleChooseTagDialogFragment.SmsScheduleChooseTagDialogInterface,
		SmsScheduleAddEditMessageFragment.SmsScheduleAddEditMessageFragmentInterface {
	public static final String TAG = SmsScheduleAddEditFragmentActivity.class
			.getName();
	private static final String LOG_TAG = SmsScheduleAddEditFragmentActivity.class
			.getName();

	public static final String ARGUMENTS_SMS_SCHEDULE_ID = "smsScheduleId";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.sms_schedule_add_edit_main);
		FragmentManager fragmentManager = getFragmentManager();
		Fragment fragment = new SmsScheduleAddEditFragment();

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		fragment.setArguments(bundle);

		fragmentManager
				.beginTransaction()
				.replace(R.id.sms_schedule_add_edit_main_content_frame,
						fragment, SmsScheduleAddEditFragment.TAG).commit();
		Log.v(LOG_TAG, "replace SmsScheduleAddEditFragment.");
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Fragment contentFragment = getFragmentManager().findFragmentById(
				R.id.sms_schedule_add_edit_main_content_frame);
		if (contentFragment instanceof SmsScheduleAddEditFragment) {
			((SmsScheduleAddEditFragment) contentFragment).insertOrUpdate();
		}
		super.onBackPressed();
	}

	@Override
	public void onSmsScheduleDateTimeDatePickerDialogPositiveClick(int year,
			int monthOfYear, int dayOfMonth) {
		SmsScheduleAddEditFragment smsScheduleAddEditFragment = (SmsScheduleAddEditFragment) getFragmentManager()
				.findFragmentByTag(SmsScheduleAddEditFragment.TAG);
		smsScheduleAddEditFragment
				.handleSmsScheduleDateTimeDatePickerDialogPositiveClick(year,
						monthOfYear, dayOfMonth);

	}

	@Override
	public void onSmsScheduleDateTimeDatePickerDialogNegativeClick() {
	}

	@Override
	public void onSmsScheduleDateTimeTimePickerDialogPositiveClick(
			int hourOfDay, int minute) {
		SmsScheduleAddEditFragment smsScheduleAddEditFragment = (SmsScheduleAddEditFragment) getFragmentManager()
				.findFragmentByTag(SmsScheduleAddEditFragment.TAG);
		smsScheduleAddEditFragment
				.handleSmsScheduleDateTimeTimePickerDialogPositiveClick(
						hourOfDay, minute);
	}

	@Override
	public void onSmsScheduleDateTimeTimePickerDialogNegativeClick() {
	}

	@Override
	public void onChooseRecipientFromChooseFromMyContacts() {
		SmsScheduleRecipientsListFragment smsScheduleRecipientsListFragment = (SmsScheduleRecipientsListFragment) getFragmentManager()
				.findFragmentById(R.id.sms_schedule_add_edit_main_content_frame);
		smsScheduleRecipientsListFragment.onChooseRecipientFromContact();

	}

	@Override
	public void onChooseRecipientsFromContactGroup() {
		SmsScheduleRecipientsListFragment smsScheduleRecipientsListFragment = (SmsScheduleRecipientsListFragment) getFragmentManager()
				.findFragmentById(R.id.sms_schedule_add_edit_main_content_frame);
		smsScheduleRecipientsListFragment.onChooseRecipientFromContactGroup();

	}

	@Override
	public void onChooseRecipientFromUserInput() {
		SmsScheduleAddRecipientsFromUserInputDialogFragment smsScheduleAddRecipientsFromUserInputDialogFragment = new SmsScheduleAddRecipientsFromUserInputDialogFragment();
		Bundle bundle = new Bundle();
		smsScheduleAddRecipientsFromUserInputDialogFragment
				.setArguments(bundle);
		smsScheduleAddRecipientsFromUserInputDialogFragment.show(
				getFragmentManager(),
				SmsScheduleAddRecipientsFromUserInputDialogFragment.TAG);
	}

	@Override
	public void onChooseSmsScheduleAddRecipientsFromUserInputDialogPositiveClick(
			String phoneNumber, boolean isEditing) {
		if (PhoneNumberUtils.isWellFormedSmsAddress(phoneNumber) == true) {
			SmsScheduleRecipientsListFragment smsScheduleRecipientsListFragment = (SmsScheduleRecipientsListFragment) getFragmentManager()
					.findFragmentById(
							R.id.sms_schedule_add_edit_main_content_frame);
			if (isEditing == true) {
				smsScheduleRecipientsListFragment
						.handleChooseRecipientFromUserInput();
			} else {
				smsScheduleRecipientsListFragment
						.handleChooseRecipientFromUserInput(phoneNumber);
			}
		}
	}

	@Override
	public void onChooseSmsScheduleAddRecipientsFromUserInputDialogNegativeClick() {
	}

	@Override
	public void onSmsScheduleRepeatEveryDialogPositiveClick(int repeatType,
			int number) {
		SmsScheduleRepeatFragment smsScheduleRepeatFragment = (SmsScheduleRepeatFragment) getFragmentManager()
				.findFragmentById(R.id.sms_schedule_add_edit_main_content_frame);
		smsScheduleRepeatFragment.handleRepeatEvery(repeatType, number);
	}

	@Override
	public void onSmsScheduleRepeatEveryDialogNegativeClick() {

	}

	@Override
	public void onSmsScheduleRepeatEndAfterDialogPositiveClick(int number) {
		SmsScheduleRepeatFragment smsScheduleRepeatFragment = (SmsScheduleRepeatFragment) getFragmentManager()
				.findFragmentById(R.id.sms_schedule_add_edit_main_content_frame);
		smsScheduleRepeatFragment.handleRepeatEnd(
				SmsScheduleContract.SmsSchedules.END_TYPE_AFTER_TIMES, number,
				null);

	}

	@Override
	public void onSmsScheduleRepeatEndAfterDialogNegativeClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSmsScheduleRepeatEndOnSpecificDateDatePickerDialogPositiveClick(
			int year, int monthOfYear, int dayOfMonth) {
		SmsScheduleRepeatFragment smsScheduleRepeatFragment = (SmsScheduleRepeatFragment) getFragmentManager()
				.findFragmentById(R.id.sms_schedule_add_edit_main_content_frame);
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, monthOfYear);
		calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
		smsScheduleRepeatFragment.handleRepeatEnd(
				SmsScheduleContract.SmsSchedules.END_TYPE_ON_SPECIFIC_DATETIME,
				null, calendar.getTimeInMillis());

	}

	@Override
	public void onSmsScheduleRepeatEndOnSpecificDateDatePickerDialogDialogNegativeClick() {

	}

	@Override
	public void onChooseSmsScheduleChooseTagDialogName() {
		SmsScheduleAddEditMessageFragment fragment = (SmsScheduleAddEditMessageFragment) getFragmentManager()
				.findFragmentByTag(SmsScheduleAddEditMessageFragment.TAG);
		fragment.handleChooseInsertNameTag();
	}

	@Override
	public void onChooseSmsScheduleChooseTagDialogFirstName() {
		SmsScheduleAddEditMessageFragment fragment = (SmsScheduleAddEditMessageFragment) getFragmentManager()
				.findFragmentByTag(SmsScheduleAddEditMessageFragment.TAG);
		fragment.handleChooseInsertFirstNameTag();
	}

	@Override
	public void onChooseSmsScheduleChooseTagDialogNickName() {
		SmsScheduleAddEditMessageFragment fragment = (SmsScheduleAddEditMessageFragment) getFragmentManager()
				.findFragmentByTag(SmsScheduleAddEditMessageFragment.TAG);
		fragment.handleChooseInsertNickNameTag();
	}

	@Override
	public void onFinishSmsScheduleAddEditMessageEditing(String result) {
		// TODO Auto-generated method stub

	}

}
